package com.cls.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cls.business.entity.DeclareTopic;
import com.cls.business.entity.Notice;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.IDeclareTopicService;
import com.cls.business.service.INoticeService;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.vo.NoticePageVO;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.IpUtil;
import com.cls.common.utils.MyUtil;
import com.cls.common.utils.StringUtils;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.session.ExpiredSessionException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 前端请求接口
 *
 * @author Administrator
 */
@Controller
@RequiredArgsConstructor
public class FrontController extends BaseController {

    private final INoticeService noticeService;
    private final ITopicPlanService topicPlanService;
    private final ISysDictService dictService;
    private final IDeclareTopicService declareTopicService;

    /**
     * 首页页面映射
     *
     * @return
     */
    @GetMapping("front/index")
    public String index(HttpServletRequest request, Model model) {
        if(IpUtil.isMSBrowser(request)){
            return "redirect:/isIE";
        }


//        通知公告
        PageInfo<Notice> pageInfo = baseIndex();
//        结果公示
        model.addAttribute("noticeList", pageInfo.getList());
        try {
            getCurrentUser();
            model.addAttribute("login", true);
        } catch (ExpiredSessionException e) {
            model.addAttribute("login", false);
        }
        return MyUtil.view("front/index");
    }


    @RequestMapping(MyConstant.VIEW_PREFIX + "front/index")
    public String baseIndex(Model model) {
        //        通知公告
        PageInfo<Notice> pageInfo = baseIndex();
//        结果公示
        model.addAttribute("noticeList", pageInfo.getList());
        return MyUtil.view("front/index");
    }


    /**
     * 首页页面映射
     *
     * @return
     */
    @GetMapping("front/getIndexResult")
    @ResponseBody
    public ResponseVO getIndexResult(Model model, Integer pageSize) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Notice::getTypeCode, Arrays.asList(DictEnum.NOTICE_TYPE_LXGG.getCode(), DictEnum.NOTICE_TYPE_NLXGG.getCode()));
        queryWrapper.orderByDesc(Notice::getIsPublish);
        queryWrapper.orderByDesc(Notice::getPublishTime);
        if (Objects.isNull(pageSize)) {
            List<Notice> notices = this.noticeService.findNotices(queryWrapper);
            return ResponseVO.success(notices);
        } else {
            //        通知公告
            QueryRequest req = new QueryRequest();
            req.setPageNum(1);
            req.setPageSize(pageSize);
            PageInfo<Notice> pageInfo = this.noticeService.findNotices(req, queryWrapper);
            return ResponseVO.success(pageInfo.getList());
        }

    }

    /**
     * 首页页面映射
     *
     * @return
     */
    @GetMapping("front/getIndexNotice")
    @ResponseBody
    public ResponseVO getIndexNotice(Model model, Integer pageSize) {
        if (Objects.isNull(pageSize)) {
            LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(Notice::getIsPublish);
            queryWrapper.orderByDesc(Notice::getPublishTime);
            queryWrapper.eq(Notice::getIsPublish, true);
            List<Notice> notices = this.noticeService.findNotices(queryWrapper);
            return ResponseVO.success(notices);
        } else {
//        通知公告
            QueryRequest req = new QueryRequest();
            req.setPageNum(1);
            req.setPageSize(8);
            Notice notice = new Notice();
            notice.setIsPublish(true);
            PageInfo<Notice> pageInfo = this.noticeService.findNotices(req, notice);
            return ResponseVO.success(pageInfo.getList());
        }
    }

    /**
     * 通知公告列表映射
     *
     * @return
     */
    @GetMapping("front/noticList")
    public String noticList(Model model) {
        try {
            getCurrentUser();
            model.addAttribute("login", true);
        } catch (ExpiredSessionException e) {
            model.addAttribute("login", false);
        }
        return MyUtil.view("front/noticList");
    }

    /**
     * 结果公示列表映射
     *
     * @return
     */
    @GetMapping("front/resultList")
    public String resultList(Model model) {
        try {
            getCurrentUser();
            model.addAttribute("login", true);
        } catch (ExpiredSessionException e) {
            model.addAttribute("login", false);
        }
        model.addAttribute("result", true);
        return MyUtil.view("front/noticList");
    }

    /**
     * 通知公告详情映射
     *
     * @return
     */
    @GetMapping("front/noticeDetail/{noticeId}")
    public String noticeDetail(@PathVariable Long noticeId, Model model) {
        try {
            getCurrentUser();
            model.addAttribute("login", true);
        } catch (ExpiredSessionException e) {
            model.addAttribute("login", false);
        }
        NoticePageVO oneHaveNextLast = noticeService.getOneHaveNextLast(noticeId);
        model.addAttribute("last", oneHaveNextLast.getPreNotice());
        model.addAttribute("next", oneHaveNextLast.getNetxNotice());
        Notice notice = oneHaveNextLast.getNotice();
        model.addAttribute("notice", notice);
        if (StringUtils.isNotBlank(notice.getTypeCode())) {
            SysDict byCode = dictService.findByCode(notice.getTypeCode());
            model.addAttribute("typeCode", byCode);
            if (notice.getTypeCode().equals(DictEnum.NOTICE_TYPE_NLXGG.getCode())) {
                model.addAttribute("nlxgg", true);
            }
            //如果是课题征集和课题指南需要关联一下
            if (DictEnum.NOTICE_TYPE_KTZJ.getCode().equals(notice.getTypeCode())) {
                TopicPlan topicPlan = topicPlanService.selectBySelectNoticeId(noticeId);
                model.addAttribute("topicPlan", topicPlan);
            } else if (DictEnum.NOTICE_TYPE_KTZN.getCode().equals(notice.getTypeCode())) {
                TopicPlan topicPlan = topicPlanService.selectByApplyNoticeId(noticeId);
                model.addAttribute("topicPlan", topicPlan);
            } else if (DictEnum.NOTICE_TYPE_NLXGG.getCode().equals(notice.getTypeCode())) {
                TopicPlan topicPlan = topicPlanService.selectByPreItemNoticeId(noticeId);
                model.addAttribute("topicPlan", topicPlan);
                List<DeclareTopic> declareTopicList = declareTopicService.reportListDeclareTopic(topicPlan.getPlanId());
                model.addAttribute("declareTopics", declareTopicList);
            } else if (DictEnum.NOTICE_TYPE_JXGG.getCode().equals(notice.getTypeCode())) {
                TopicPlan topicPlan = topicPlanService.selectByConcludeNoticeId(noticeId);
                model.addAttribute("topicPlan", topicPlan);
            } else if (DictEnum.NOTICE_TYPE_RESULT_JXGG.getCode().equals(notice.getTypeCode())) {
                TopicPlan topicPlan = topicPlanService.selectNoticeByConcludeFinishNoticeId(noticeId);
                model.addAttribute("topicPlan", topicPlan);
            }
        }
        return MyUtil.view("front/noticeDetail");
    }

    public PageInfo<Notice> baseIndex() {
        QueryRequest req = new QueryRequest();
        req.setPageNum(1);
        req.setPageSize(8);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getIsPublish);
        queryWrapper.orderByDesc(Notice::getPublishTime);
        return this.noticeService.findNotices(req, queryWrapper);
    }
}
